#include "framelesswidget.h"

#include <QHBoxLayout>
#include <QPushButton>
#include <QMouseEvent>

FramelessWidget::FramelessWidget(QWidget *parent): QWidget(parent)
{
    // 设置最小宽高
    this->setMinimumWidth(600);
    this->setMinimumHeight(500);

    // 横向布局
    QHBoxLayout* horialzonLayout = new QHBoxLayout(this);
    horialzonLayout->setSpacing(10);

    // 确认按钮
    QPushButton* btnConfirm = new QPushButton(this);
    btnConfirm->setText("确认");

    // 取消按钮
    QPushButton* btnCancel = new QPushButton(this);
    btnCancel->setText("取消");

    horialzonLayout->addWidget(btnConfirm);
    horialzonLayout->addWidget(btnCancel);

    QString style = R"(
        QPushButton {
            background: blue;
            font-size: 22px;
            padding: 10px;
            color: #FFFFFF
        }

        QPushButton:hover {
            background: red;
        }

        QPushButton:pressed {
            background: red;
        }
    )";

    // 设置样式
    btnConfirm->setStyleSheet(style);
    btnCancel->setStyleSheet(style);

    // 去除标题栏
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint);

    isLeftPressed = false;

    // 让控件能够追踪鼠标的移动情况
    this->setMouseTracking(true);
}

FramelessWidget::~FramelessWidget() {}

/**
 * 重写鼠标按下事件
 */
void FramelessWidget::mousePressEvent(QMouseEvent *event)
{
    switch (event->button()) {
        case Qt::RightButton:
            this->close();
            break;
        case Qt::LeftButton:
            isLeftPressed = true;

            if(location == CENTER) { // 位于中间的时候，才触发移动
                mouseOffset = event->globalPos() - this->frameGeometry().topLeft();
            }
            break;
    }
}

/**
 * 重写鼠标移动事件
 */
void FramelessWidget::mouseMoveEvent(QMouseEvent *event)
{
    QPoint globalPos = event->globalPos();

    QRect rect = this->rect();
    QPoint topLeft = mapToGlobal(rect.topLeft());
    QPoint bottomRight = mapToGlobal(rect.bottomRight());

    // 1. 鼠标未按下
    if(!isLeftPressed) {
        this->setCursorShape(globalPos);
        return;
    }

    // 2. 鼠标在Center位置按下
    if(location == CENTER) {
        move(globalPos - mouseOffset);
        event->accept();
        return;
    }

    // 3. 缩放
    QRect rMove(topLeft, bottomRight);

    switch (location) {
        case TOP:
            rMove.setY(globalPos.y());
            break;
        case BOTTOM:
            rMove.setHeight(globalPos.y() - topLeft.y());
            break;
        case LEFT:
            rMove.setX(globalPos.x());
            break;
        case RIGHT:
            rMove.setWidth(globalPos.x() - topLeft.x());
            break;
        case TOP_LEFT:

            break;
        case TOP_RIGHT:

            break;
        case BOTTOM_LEFT:

            break;
        case BOTTOM_RIGHT:

            break;
        default:
            break;
    }

    this->setGeometry(rMove);
}

/**
 * 重写鼠标的释放事件
 */
void FramelessWidget::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton) {
        isLeftPressed = false;
    }
}

/**
 * 设置鼠标的形状
 */
void FramelessWidget::setCursorShape(const QPoint &point)
{
    QRect rect = this->rect();
    QPoint topLeft = mapToGlobal(rect.topLeft());
    QPoint topRight = mapToGlobal(rect.topRight());
    QPoint bottomLeft = mapToGlobal(rect.bottomLeft());
    QPoint bottomRight = mapToGlobal(rect.bottomRight());

    int x = point.x();
    int y = point.y();

    if(x >= topLeft.x() && x <= topLeft.x() + 5 && y >= topLeft.y() && y <= topLeft.y() + 5) {
        // 左上角
        location = TOP_LEFT;
        this->setCursor(QCursor(Qt::SizeFDiagCursor));
    } else if(x <= bottomRight.x() && x >= bottomRight.x() - 5 && y <= bottomRight.y() && y >= bottomRight.y() - 5) {
        // 右下角
        location = BOTTOM_RIGHT;
        this->setCursor(QCursor(Qt::SizeFDiagCursor));
    } else if(x <= topRight.x() && x >= topRight.x() - 5 && y >= topRight.y() && y <= topRight.y() + 5) {
        // 右上角
        location = TOP_RIGHT;
        this->setCursor(QCursor(Qt::SizeBDiagCursor));
    } else if(x >= bottomLeft.x() && x <= bottomLeft.x() + 5 && y <= bottomLeft.y() && y >= bottomLeft.y() - 5) {
        // 左下角
        location = BOTTOM_LEFT;
        this->setCursor(QCursor(Qt::SizeBDiagCursor));
    } else if(y >= topLeft.y() && y <= topLeft.y() + 5) {
        // 上边
        location = TOP;
        this->setCursor(QCursor(Qt::SizeVerCursor));
    } else if(y <= bottomLeft.y() && y >= bottomLeft.y() - 5) {
        // 下边
        location = BOTTOM;
        this->setCursor(QCursor(Qt::SizeVerCursor));
    } else if(x >= topLeft.x() && x <= topLeft.x() + 5) {
        // 左边
        location = LEFT;
        this->setCursor(QCursor(Qt::SizeHorCursor));
    } else if(x <= bottomRight.x() && x >= bottomRight.x() - 5) {
        // 右边
        location = RIGHT;
        this->setCursor(QCursor(Qt::SizeHorCursor));
    }
    else {
        location = CENTER;
        this->setCursor(QCursor(Qt::ArrowCursor));
    }
}
